﻿Namespace Core
    Class Render

        ''' <summary>
        ''' Loads a thumbnail image from Internet or hardrive and gets a BitmapSource
        ''' </summary>
        ''' <param name="Uri">The uri of the Image to load</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function LoadThumbnail(ByVal Uri As Uri) As BitmapSource
            Dim Decoder As BitmapDecoder = BitmapDecoder.Create(Uri,
                                                                 BitmapCreateOptions.None,
                                                                 BitmapCacheOption.None)

            Dim image As BitmapSource

            If Decoder.Frames(0).Thumbnail Is Nothing Then
                image = __LoadImage(Uri, 150, 150)
            Else
                image = Decoder.Frames(0).Thumbnail
            End If

            Return image

        End Function

        ''' <summary>
        ''' Uses an Uri to get a BitmapSource and load an image
        ''' </summary>
        ''' <param name="Source">The Uri source of the image to load</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function LoadImage(ByVal Source As Uri,
                                         Optional ByVal CacheOption As BitmapCacheOption = BitmapCacheOption.None
                                                                                           ) As BitmapSource
            Dim Decoder As BitmapDecoder = BitmapDecoder.Create(Source,
                                                              BitmapCreateOptions.None,
                                                              CacheOption)

            Return Decoder.Frames(0)

        End Function

        Private Shared Function __LoadImage(ByVal Uri As Uri, _
                                     ByVal PixelHeight As Integer, ByVal PixelWidth As Integer) As BitmapSource

            Dim bm1 As New Windows.Media.Imaging.BitmapImage
            bm1.BeginInit()

            bm1.UriSource = Uri
            If PixelHeight > 0 Then
                bm1.DecodePixelHeight = PixelHeight
            End If
            If PixelWidth > 0 Then
                bm1.DecodePixelHeight = PixelWidth
            End If
            bm1.EndInit()
            Return bm1
        End Function

        ''' <summary>
        ''' Parse an string containig Xaml data and retuns an usable object
        ''' </summary>
        ''' <typeparam name="T">The type to use</typeparam>
        ''' <param name="XamlString">An string containing Xaml Data</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ParseXaml(Of T)(ByVal XamlString As String) As T
            Dim strReader As New System.IO.StringReader(XamlString)

            Dim xmlReader As Xml.XmlReader = Xml.XmlReader.Create(strReader)
            Dim Loader As T = CType(System.Windows.Markup.XamlReader.Load(xmlReader), T)

            Return Loader
        End Function

        ''' <summary>
        ''' Loads a file containing Xaml data and returns an usable object
        ''' </summary>
        ''' <typeparam name="T">The type to use</typeparam>
        ''' <param name="file">A files conaining Xaml data</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function LoadXaml(Of T)(ByVal file As String) As T
            Dim StreamReader As New IO.StreamReader(file)
            Dim XamlString As String = StreamReader.ReadToEnd

            Dim XmlReader As Xml.XmlReader = Xml.XmlReader.Create(New IO.StringReader(XamlString))
            Dim Loaded As T = CType(System.Windows.Markup.XamlReader.Load(XmlReader), T)

            Return Loaded

        End Function

    End Class
End Namespace
